package solution.design;

import java.util.Stack;

/**
 * @author zhangmin
 * @create 2021-08-11 13:46
 * 设计问题--最小栈
 * 当push的元素比min还小时，将min与当前元素一起push进去，这样当pop时，pop到min时，可以将min退回到前一个min
 */
public class MinStack {
    int min=Integer.MAX_VALUE;
    Stack<Integer> stack=new Stack<>();

    /** initialize your data structure here. */
    public MinStack() {
    }

    public void push(int val) {
        if (val<=this.min){
            this.stack.push(min);
            min=val;
        }
        stack.push(val);
    }

    public void pop() {
        if (stack.pop()==min)
            min=stack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return this.min;
    }
}
